﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;

using Museum.DBAccess;

public partial class Login : System.Web.UI.Page
{
    //for logging to file
    log4net.ILog logger = log4net.LogManager.GetLogger("File");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            logger.Info("Starting page load of Login.aspx");
            string strWatermarkSearch = "Username...";
            txtUsername.Text = strWatermarkSearch;

            txtUsername.Attributes.Add("onfocus", "WatermarkFocus(this, '" + strWatermarkSearch + "');");
            txtUsername.Attributes.Add("onblur", "WatermarkBlur(this, '" + strWatermarkSearch + "');");
        }
    }

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        //Business logic: validate user credentials in user table of DB if admin credentials match else museum owner.
        int museumId = 0;
        int schoolId = 0;

        if (!string.IsNullOrEmpty(txtUsername.Text.Trim()) && !string.IsNullOrEmpty(txtPassword.Text.Trim()))
        {
            Session["Username"] = txtUsername.Text.Trim();
            //check credentials in DB and assign master page dynamically
            if (ddlRole.SelectedValue.Equals("2"))
                schoolId = DBAccess.IsUserValid(txtUsername.Text.Trim(), txtPassword.Text.Trim(), Convert.ToInt32(ddlRole.SelectedValue));
            else
                museumId = DBAccess.IsUserValid(txtUsername.Text.Trim(), txtPassword.Text.Trim(), Convert.ToInt32(ddlRole.SelectedValue));

            if (museumId > 0 || schoolId > 0)
            {
                SaveUserToCookie(txtUsername.Text.Trim(), txtPassword.Text.Trim());
                //redirect user to museum owner dashboard
                if (ddlRole.SelectedValue.Equals("2"))
                {
                    Session["SchoolId"] = schoolId.ToString();
                    DataTable dtSchoolDetails = DBAccess.GetSchoolDetails(schoolId);
                    if (dtSchoolDetails != null && dtSchoolDetails.Rows.Count > 0)
                    {
                        Session["MuseumId"] = dtSchoolDetails.Rows[0]["SchoolMuseumId"].ToString();
                    }
                    Session["Role"] = ddlRole.SelectedValue;
                    Response.Redirect("ManageGame.aspx", false);
                }
                else if (ddlRole.SelectedValue.Equals("0"))
                {
                    Session["Role"] = ddlRole.SelectedValue;
                    Response.Redirect("ManageMuseum.aspx", false);
                }
                else
                {
                    Session["Role"] = ddlRole.SelectedValue;
                    Session["MuseumId"] = museumId.ToString();
                    Response.Redirect("ManageGame.aspx", false);
                }
            }
            else
            {
                lblError.Visible = true;
                lblError.Text = "Invalid credentials.";
            }
        }
    }

    private void SaveUserToCookie(string username, string password)
    {
        HttpCookie usernameCookie = new HttpCookie("username");
        usernameCookie.Value = txtUsername.Text.Trim();
        HttpCookie passwordCookie = new HttpCookie("password");
        passwordCookie.Value = txtPassword.Text.Trim();
    }
}
